///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ZZ_ZCAPTURE_H_
#define _BUOLA_ZZ_ZCAPTURE_H_

#include <buola/zz/zbase.h>

namespace buola { namespace zz {

template<typename tA,typename tAttr>
class ZCapture : public ZBase<ZCapture<tA,tAttr>>
{
public:
    template<typename tIt>
    struct tAttribute
    {
        typedef UUnused tType;
    };

    ZCapture(const ZCapture &pRH)
        :   mA(pRH.mA)
        ,   mAttr(pRH.mAttr)
    {}
    
    ZCapture(ZCapture &&pRH)
        :   mA(std::move(pRH.mA))
        ,   mAttr(pRH.mAttr)
    {}
    
    template<typename tPA>
    explicit ZCapture(tPA &&pA,tAttr &pAttr)
        :   mA(std::forward<tPA>(pA))
        ,   mAttr(pAttr)
    {}
    
    template<typename tIt,typename tAttrUnused,typename tCtx,typename tSk>
    bool Parse(tIt &pB,const tIt &pE,tAttrUnused&,tCtx &pCtx,const tSk &pSk) const
    {
        return mA.Parse(pB,pE,mAttr,pCtx,pSk);
    }

private:    
    tA mA;
    tAttr &mAttr;
};

template<typename tA,typename tAttr>
class ZCaptureInto : public ZBase<ZCaptureInto<tA,tAttr>>
{
public:
    template<typename tIt>
    struct tAttribute
    {
        typedef UUnused tType;
    };

    ZCaptureInto(const ZCaptureInto &pRH)
        :   mA(pRH.mA)
        ,   mAttr(pRH.mAttr)
    {}
    
    ZCaptureInto(ZCaptureInto &&pRH)
        :   mA(std::move(pRH.mA))
        ,   mAttr(pRH.mAttr)
    {}
    
    template<typename tPA>
    explicit ZCaptureInto(tPA &&pA,tAttr &pAttr)
        :   mA(std::forward<tPA>(pA))
        ,   mAttr(pAttr)
    {}
    
    template<typename tIt,typename tAttrUnused,typename tCtx,typename tSk>
    bool Parse(tIt &pB,const tIt &pE,tAttrUnused&,tCtx &pCtx,const tSk &pSk) const
    {
        typename tAttr::value_type lAttr;
        bool lOk=mA.Parse(pB,pE,lAttr,pCtx,pSk);
        if(lOk)
            mAttr.emplace_back(std::move(lAttr));
        return lOk;
    }

private:    
    tA mA;
    tAttr &mAttr;
};

template<typename tA>
class CAttributeCapture
{
public:
    CAttributeCapture(tA &&pA)
        :   mA((tA&&)pA)
    {}
    
    template<typename tAttr>
    ZCapture<ODecay<tA>,tAttr>
    operator()(tAttr &pAttr)
    {
        return ZCapture<ODecay<tA>,tAttr>(std::forward<tA>(mA),pAttr);
    }
    
    template<typename tAttr>
    ZCaptureInto<ODecay<tA>,tAttr>
    operator[](tAttr &pAttr)
    {
        return ZCaptureInto<ODecay<tA>,tAttr>(std::forward<tA>(mA),pAttr);
    }
    
    tA &&mA;
};
    
/*namespace zz*/ } /*namespace buola*/ }

#endif
